#!/bin/bash

software_dir="stable-diffusion"
venv_name="amd${software_dir}"
RUN_SCRIPT="run_${software_dir}.sh"

sudo apt install wget git python3 python3-venv libgl1 libglib2.0-0 -y
#下载文件
[ ! -d "$software_dir" ] && git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git $software_dir
cd $software_dir
[ ! -d "$venv_name" ] && python3 -m venv $venv_name
source $venv_name/bin/activate

# 安装依赖
pip3 install --upgrade pip
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/rocm6.2.4
#pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/rocm5.7
#pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/rocm5.4.2
#sed -i '/torchaudio==2.0.2/d' requirements.txt
#sed -i '/torchvision==0.15.2/d' requirements.txt
#sed -i '/torch/d' requirements.txt
#sed -i '/#--extra-index-url https:\/\/download.pytorch.org\/whl\/cu118/#d' requirements.txt

pip3 install -r requirements.txt
#pip3 install gradio==3.50.2

echo "建立启动脚本"
if [ ! -f "$RUN_SCRIPT" ]; then
    cat > $RUN_SCRIPT <<EOF
#!/bin/bash
source $venv_name/bin/activate
export HSA_OVERRIDE_GFX_VERSION=11.0.0
export HIP VISIBLE DEVICES=0
python3 launch.py --lowvram --precision full --no-half  --autolaunch --listen --theme dark --skip-torch-cuda-test
EOF
    chmod +x $RUN_SCRIPT
fi

echo "安装完成"
